package com.xinzhang;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Timer;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;

import com.helper.SqlLiteHepler;
import com.information.UploadImage;
import com.tool.WebSeviceHelper;
import com.tool.tool;
import com.xinzhang.base.BaseActiveGruop;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.app.Service;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.Uri;
import android.os.Environment;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;

public class uploadService extends Service {

	
	private Timer timer;
	
	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}

	public int GetisConnection() {
		ConnectivityManager conMan = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
		android.net.NetworkInfo.State mobile = conMan.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();
		android.net.NetworkInfo.State wifi = conMan.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();
		int i=1;
        if(mobile==android.net.NetworkInfo.State.CONNECTED||mobile==android.net.NetworkInfo.State.CONNECTING)
        {
        	i=0;
    	}
        if(wifi==android.net.NetworkInfo.State.CONNECTED||wifi==android.net.NetworkInfo.State.CONNECTING)
        {
        	i=0;
        }
        return i;
	}
	
	class MyTask extends java.util.TimerTask{
        public void run() {
        	
        	if(uploadService.this.GetisConnection()==1)
        	{
        		return;
        	}
        	else {
	        		SqlLiteHepler sqlLiteHepler=new SqlLiteHepler(uploadService.this);
	            	List<String> files=sqlLiteHepler.GetUploadFiles();
	            	sqlLiteHepler.Close();
	            	for (int i = 0; i < files.size(); i++) {
	            		sqlLiteHepler=new SqlLiteHepler(uploadService.this);
	            		UploadImage uploadImage=sqlLiteHepler.GetUploadImages(files.get(i));
	            		JSONObject jsonObject=new JSONObject();
	            		try {
	    					jsonObject.put("FileName",uploadImage.getFilename());
	    					jsonObject.put("Title",uploadImage.getTitle() );
	    					jsonObject.put("Desc", uploadImage.getImagedesc());
	    					jsonObject.put("UserId", uploadImage.getUserid());
	    					jsonObject.put("X", uploadImage.getX());
	    					jsonObject.put("Y", uploadImage.getY());
	    					String jsonString= jsonObject.toString();
	    					Map<String, Object> map=new HashMap<String, Object>();
	    					map.put("json", jsonString);
	    					map.put("data", uploadImage.getData());
	    					Object obj= WebSeviceHelper.getKSoapData("Uploadimage", map);
	    					int result=Integer.valueOf(obj.toString());
	    					if(result>0)
	    					{
	    						sqlLiteHepler.DeleteUploadImages(files.get(i));
	    					}
	    					
	    				} catch (JSONException e) {
	    					// TODO Auto-generated catch block
	    					e.printStackTrace();
	    				} catch (Exception e) {
	    					// TODO Auto-generated catch block
	    					e.printStackTrace();
	    				}
	    				finally
	    				{
	    					sqlLiteHepler.Close();
	    				}
	
	    			}
	            	
	            }
			}
    }
	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();
//		timer=new Timer();
//		timer.schedule(new MyTask(), 60 * 2000);
		
		
		
	}
	
	
	
	
	
	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		
	}
	
	@Override
	public void onStart(Intent intent, int startId) {
		// TODO Auto-generated method stub
		super.onStart(intent, startId);
	}
	
	
}
